package xyz.xtt.asset.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.tongtong.share.client.ShareClient;
import com.tongtong.share.request.AssistantMessageRequest;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.service.IInnerService;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.equity.client.EquityClient;
import xyz.xtt.equity.client.request.BlockAssetNoBenefitRequest;
import xyz.xtt.equity.client.request.SendAssetNoBenefitRequest;
import xyz.xtt.equity.client.request.SendDeliverResultRequest;
import xyz.xtt.equity.client.request.SendFreeBenefitPackageRequest;
import xyz.xtt.equity.client.request.UnBlockAssetNoBenefitRequest;
import xyz.xtt.exchange.client.ExchangeClient;
import xyz.xtt.exchange.request.CopyrightRequest;
import xyz.xtt.exchange.response.AssetAuditResponse;
import xyz.xtt.pay.client.PayClient;
import xyz.xtt.pay.client.response.BusinessOrderDto;
import xyz.xtt.pay.client.response.BusinessOrderResponse;
import xyz.xtt.pay.client.response.TblPayOrderDto;
import xyz.xtt.user.client.UserClient;
import xyz.xtt.user.client.request.UserRegRequest;
import xyz.xtt.user.client.response.UserInfoResponse;
import xyz.xtt.user.client.response.UserRegResponse;

@Slf4j
@Service
public class InnerServiceImpl implements IInnerService {
	@Autowired
	private UserClient userClient;
	@Autowired
	private PayClient payClient;
	@Autowired
	private EquityClient equityClient;
	@Autowired
	private ExchangeClient exchangeClient;
	@Autowired
	private ShareClient shareClient;

	@Override
	public UserInfoResponse userInfo(Long appUid) {
		ApiResult<UserInfoResponse> response = userClient.info(appUid);
		log.info("userClient userInfo one response:{}", JSONUtil.toJsonStr(response));
		UserInfoResponse info = Optional.ofNullable(response).filter(ApiResult::isOk).map(ApiResult::getData).orElse(null);
		return info;
	}

	@Override
	public UserRegResponse register(UserRegRequest userRegRequest) {
		ApiResult<UserRegResponse> response = userClient.register(userRegRequest);
		log.info("userClient register one response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public List<UserInfoResponse> userInfo(Collection<Long> appUid) {
		ApiResult<List<UserInfoResponse>> response = userClient.batchInfo(appUid);
		log.info("userClient userInfo batch response:{}", JSONUtil.toJsonStr(response));
		List<UserInfoResponse> info = Optional.ofNullable(response).filter(ApiResult::isOk).map(ApiResult::getData).orElse(Lists.newArrayList());
		return info;
	}

	@Override
	public UserInfoResponse userInfoWithPhone(String phone) {
		ApiResult<UserInfoResponse> response = userClient.infoWithPhone(phone);
		log.info("userClient userInfoWithPhone one response:{}", JSONUtil.toJsonStr(response));
		UserInfoResponse info = Optional.ofNullable(response).filter(ApiResult::isOk).map(ApiResult::getData).orElse(null);
		return info;
	}

	@Override
	public Boolean mintAfterUpdateResult(Long payOrderId, Long transactionOrderId) {
		ApiResult<Boolean> response = payClient.mintAfterUpdateResult(payOrderId, transactionOrderId);
		log.info("payClient userInfo one response:{}", JSONUtil.toJsonStr(response));
		Boolean orElse = Optional.ofNullable(response).filter(ApiResult::isOk).map(ApiResult::getData).orElse(Boolean.FALSE);
		return orElse;
	}

	@Override
	public TblPayOrderDto fetchPayOrder(Long payOrderId) {
		ApiResult<TblPayOrderDto> response = payClient.fetchPayOrder(payOrderId);
		log.info("payClient userInfo one response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Boolean deliverCheck(Long entitlementId, Long uid) {
		ApiResult<Boolean> response = equityClient.deliverCheck(entitlementId, uid);
		log.info("payClient userInfo one response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Map<Long, BusinessOrderDto> getBusinessListByIds(Set<Long> refIds) {
		ApiResult<Map<Long, BusinessOrderDto>> response = payClient.getBusinessListByIds(refIds);
		log.info("payClient getBusinessListByIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Maps.newHashMap());
	}

	@Override
	public BusinessOrderResponse businessOrderInfoWithId(Long businessOrderId) {
		ApiResult<BusinessOrderResponse> response = payClient.businessOrderInfoWithId(businessOrderId);
		log.info("payClient getBusinessListByIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public BusinessOrderDto getBusinessOrderById(Long orderById) {
		Map<Long, BusinessOrderDto> businessOrderMap = getBusinessListByIds(Sets.newHashSet(orderById));
		if (CollectionUtil.isEmpty(businessOrderMap)) {
			log.info("orderById: {} query is null", orderById);
			return null;
		}
		BusinessOrderDto businessOrderDto = businessOrderMap.get(orderById);
		return businessOrderDto;
	}

	@Override
	public AssetAuditResponse assetAuditInfo(Long assetAuditId) {
		ApiResult<AssetAuditResponse> response = exchangeClient.assetAuditInfo(assetAuditId);
		log.info("exchangeClient assetAuditInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Boolean assetAuditPayMint(Long assetAuditId, Long assetId) {
		ApiResult<Boolean> response = exchangeClient.assetAuditPayMint(assetAuditId, assetId);
		log.info("exchangeClient assetAuditPayMint response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean judgeMerchantPer(String uid) {
		ApiResult<Boolean> response = exchangeClient.judgeMerchantPer(uid);
		log.info("exchangeClient judgeMerchantPer response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean installByPackageIds(String packageIds, Long assetId) {
		ApiResult<Boolean> response = equityClient.installByPackageIds(packageIds, assetId);
		log.info("equityClient installByPackageIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean handleOrderPaySuccess(TblPayOrderDto payOrderDto) {
		ApiResult<Boolean> response = userClient.handleOrderPaySuccess(payOrderDto);
		log.info("userClient handleOrderPaySuccess response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean sendFreeBenefitPackage(SendFreeBenefitPackageRequest request) {
		ApiResult<Boolean> response = equityClient.sendFreeBenefitPackage(request);
		log.info("equityClient sendFreeBenefitPackage response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean sendAssetNoBenefit(SendAssetNoBenefitRequest request) {
		ApiResult<Boolean> response = equityClient.sendAssetNoBenefit(request);
		log.info("equityClient sendAssetNoBenefit response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean blockAssetNoBenefit(BlockAssetNoBenefitRequest blockAssetNoBenefitRequest) {
		ApiResult<Boolean> response = equityClient.blockAssetNoBenefit(blockAssetNoBenefitRequest);
		log.info("equityClient blockAssetNoBenefit response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean unBlockAssetNoBenefit(UnBlockAssetNoBenefitRequest unBlockAssetNoBenefitRequest) {
		ApiResult<Boolean> response = equityClient.unBlockAssetNoBenefit(unBlockAssetNoBenefitRequest);
		log.info("equityClient blockAssetNoBenefit response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean saveCopyright(CopyrightRequest copyrightRequest) {
		ApiResult<Boolean> response = exchangeClient.saveCopyright(copyrightRequest);
		log.info("exchangeClient saveCopyright response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean sendAssistantMsg(AssistantMessageRequest batchMsg) {
		ApiResult<Boolean> response = shareClient.sendAssistantMsg(batchMsg);
		log.info("shareClient sendAssistantMsg response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public Boolean saveDeliverResult(SendDeliverResultRequest request) {
		ApiResult<Boolean> response = equityClient.saveDeliverResult(request);
		log.info("equityClient saveDeliverResult response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}
}
